package com.zhao.service.serviceImpl;

import com.zhao.Others.DateAndTimeObject;
import com.zhao.Others.GenerateObject;
import com.zhao.dao.EbickDao;
import com.zhao.dao.EbickGoodsDao;
import com.zhao.dao.OrderFormDao;
import com.zhao.dao.StudentDao;
import com.zhao.pojo.*;
import com.zhao.service.OrderFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class OrderFormServiceImpl implements OrderFormService {

    @Autowired
    private EbickGoodsDao ebickGoodsDao;

    @Autowired
    private OrderFormDao orderFormDao;

    @Autowired
    private EbickDao ebickDao;

    @Autowired
    private StudentDao studentDao;

    @Override
    public String studentSubmitOrder(Student student, OrderForm orderForm, HttpSession session) {

        //OrderForm orderForm1 = orderFormDao.findByBuyerIDAndStatus(student.getStudentID(),"等待确定",)

        orderForm.setBuyerID(student.getStudentID());
        orderForm.setUniversityID(student.getUniversityID());
        orderForm.setStatus("等待确定");

        EbickGoods ebickGoods = ebickGoodsDao.findByEbickID(orderForm.getEbickID(),student.getUniversityID());

        if(ebickGoods == null){
            return "系统错误";
        }

        orderForm.setSellerID(ebickGoods.getStudentID());
        if(orderForm.getSellerID().equals(orderForm.getBuyerID())){
            return "不能买自己的商品";
        }



        orderForm.setPrice(ebickGoods.getPrice());
        orderForm.setDate(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());
        orderForm.setOrderFormID(GenerateObject.getOrderFormID(ebickGoods.getStudentID(),student.getStudentID(),ebickGoods.getEbickID()));

        OrderForm orderForm1 = orderFormDao.findByID(orderForm.getOrderFormID(),orderForm.getUniversityID());
        if(orderForm1!=null){
            if(orderForm1.getStatus().equals("已拒绝")||orderForm1.getStatus().equals("已退款")){
                int res1  = orderFormDao.delete(orderForm1.getOrderFormID(),orderForm1.getUniversityID());
                if(res1!=1){
                    return "系统错误";
                }
            }else{
                return "该订单已经存在";
            }

        }

        System.out.println(orderForm);
        int res = orderFormDao.add(orderForm);
        if(res!=1){
            return "提交失败";
        }

        return "success";
    }

    private OrderFormImpl buyorderFormToOrderFormImpl(OrderForm orderForm){
        OrderFormImpl orderForm1 = new OrderFormImpl();
        Ebick ebick = ebickDao.findByID(orderForm.getEbickID(),orderForm.getUniversityID());
        orderForm1.setEbick(ebick);

        EbickGoods ebickGoods = ebickGoodsDao.findByEbickID(ebick.getEbickID(),ebick.getUniversityID());
        orderForm1.setEbickGoods(ebickGoods);

        orderForm1.setOrderForm(orderForm);

        Student seller = studentDao.findByStudentID(ebick.getNowOwnerID(),ebick.getUniversityID());
        orderForm1.setSeller(seller);

        return orderForm1;
    }

    private OrderFormImpl sellorderFormToOrderFormImpl(OrderForm orderForm){
        OrderFormImpl orderForm1 = new OrderFormImpl();
        Ebick ebick = ebickDao.findByID(orderForm.getEbickID(),orderForm.getUniversityID());
        orderForm1.setEbick(ebick);

        EbickGoods ebickGoods = ebickGoodsDao.findByEbickID(ebick.getEbickID(),ebick.getUniversityID());
        orderForm1.setEbickGoods(ebickGoods);

        orderForm1.setOrderForm(orderForm);

        Student seller = studentDao.findByStudentID(ebick.getNowOwnerID(),ebick.getUniversityID());
        orderForm1.setSeller(seller);

        Student buyer = studentDao.findByStudentID(orderForm.getBuyerID(),orderForm.getUniversityID());
        orderForm1.setBuyer(buyer);

        return orderForm1;
    }


    @Override
    public String studentGetAllMyBuyOrders(Student student, HttpSession session) {

        //待付款
        List<OrderForm> waitPayOrderList = orderFormDao.findByBuyerIDAndStatus(student.getStudentID(),"待付款",student.getUniversityID());
        List<OrderFormImpl> waitPayOrderImplList = new ArrayList<>();
        for(OrderForm orderForm : waitPayOrderList){
            waitPayOrderImplList.add(buyorderFormToOrderFormImpl(orderForm));
        }
        session.setAttribute("waitPayOrderImplList",waitPayOrderImplList);

        //待确认收货
        List<OrderForm> waitReceiveOrderList = orderFormDao.findByBuyerIDAndStatus(student.getStudentID(),"待收货",student.getUniversityID());
        List<OrderFormImpl> waitReceiveOrderImplList = new ArrayList<>();
        for(OrderForm orderForm : waitReceiveOrderList){
            waitReceiveOrderImplList.add(buyorderFormToOrderFormImpl(orderForm));
        }
        session.setAttribute("waitReceiveOrderImplList",waitReceiveOrderImplList);

        //待确认
        List<OrderForm> waitConfirmOrderList = orderFormDao.findByBuyerIDAndStatus(student.getStudentID(),"等待确定",student.getUniversityID());
        List<OrderFormImpl> waitConfirmOrderImplList = new ArrayList<>();
        for(OrderForm orderForm : waitConfirmOrderList){
            waitConfirmOrderImplList.add(buyorderFormToOrderFormImpl(orderForm));
        }
        session.setAttribute("waitConfirmOrderImplList",waitConfirmOrderImplList);

        //已完成
        List<OrderForm> finishedOrderList = orderFormDao.findByBuyerIDAndStatus(student.getStudentID(),"已完成",student.getUniversityID());
        List<OrderFormImpl> finishedOrderImplList = new ArrayList<>();
        for(OrderForm orderForm : finishedOrderList){
            finishedOrderImplList.add(buyorderFormToOrderFormImpl(orderForm));
        }
        finishedOrderList = orderFormDao.findByBuyerIDAndStatus(student.getStudentID(),"已退款",student.getUniversityID());
        for(OrderForm orderForm : finishedOrderList){
            finishedOrderImplList.add(buyorderFormToOrderFormImpl(orderForm));
        }
        session.setAttribute("finishedOrderImplList",finishedOrderImplList);

        //已拒绝
        List<OrderForm> rejectedOrderList = orderFormDao.findByBuyerIDAndStatus(student.getStudentID(),"已拒绝",student.getUniversityID());
        List<OrderFormImpl> rejectedOrderImplList = new ArrayList<>();
        for(OrderForm orderForm : rejectedOrderList){
            rejectedOrderImplList.add(buyorderFormToOrderFormImpl(orderForm));
        }
        session.setAttribute("rejectedOrderImplList",rejectedOrderImplList);

        return "success";
    }

    @Override
    public String studentGetAllMySellOrders(Student student, HttpSession session) {

        //待发货
        List<OrderForm> waitSendOrderList = orderFormDao.findBySellerIDAndStatus(student.getStudentID(),"待收货",student.getUniversityID());
        List<OrderFormImpl> waitSendOrderImplList = new ArrayList<>();
        for(OrderForm orderForm : waitSendOrderList){
            waitSendOrderImplList.add(sellorderFormToOrderFormImpl(orderForm));
        }
        session.setAttribute("waitSendOrderImplList",waitSendOrderImplList);

        //待付款
        List<OrderForm> waitPayOrderList = orderFormDao.findBySellerIDAndStatus(student.getStudentID(),"待付款",student.getUniversityID());
        List<OrderFormImpl> waitPayOrderImplList = new ArrayList<>();
        for(OrderForm orderForm : waitPayOrderList){
            waitPayOrderImplList.add(sellorderFormToOrderFormImpl(orderForm));
        }
        session.setAttribute("waitPayOrderImplList",waitPayOrderImplList);

        //待确认
        List<OrderForm> waitConfirmOrderList = orderFormDao.findBySellerIDAndStatus(student.getStudentID(),"等待确定",student.getUniversityID());
        List<OrderFormImpl> waitConfirmOrderImplList = new ArrayList<>();
        for(OrderForm orderForm : waitConfirmOrderList){
            waitConfirmOrderImplList.add(sellorderFormToOrderFormImpl(orderForm));
        }
        System.out.println(waitConfirmOrderImplList);
        session.setAttribute("waitConfirmOrderImplList",waitConfirmOrderImplList);

        //已完成
        List<OrderForm> finishedOrderList = orderFormDao.findBySellerIDAndStatus(student.getStudentID(),"已完成",student.getUniversityID());
        List<OrderFormImpl> finishedOrderImplList = new ArrayList<>();
        for(OrderForm orderForm : finishedOrderList){
            finishedOrderImplList.add(sellorderFormToOrderFormImpl(orderForm));
        }
        finishedOrderList = orderFormDao.findBySellerIDAndStatus(student.getStudentID(),"已退款",student.getUniversityID());
        for(OrderForm orderForm : finishedOrderList){
            finishedOrderImplList.add(sellorderFormToOrderFormImpl(orderForm));
        }
        session.setAttribute("finishedOrderImplList",finishedOrderImplList);

        //已拒绝
        List<OrderForm> rejectedOrderList = orderFormDao.findBySellerIDAndStatus(student.getStudentID(),"已拒绝",student.getUniversityID());
        List<OrderFormImpl> rejectedOrderImplList = new ArrayList<>();
        for(OrderForm orderForm : rejectedOrderList){
            rejectedOrderImplList.add(sellorderFormToOrderFormImpl(orderForm));
        }
        session.setAttribute("rejectedOrderImplList",rejectedOrderImplList);

        return "success";
    }

    @Override
    public String studentCancelBuyOrder(Student student, String orderFormID, HttpSession session) {

        int res = orderFormDao.delete(orderFormID,student.getUniversityID());
        if(res!=1){
            return "error";
        }

        return "success";
    }

    @Override
    public String studentRejectOrder(Student student, String orderFormID, HttpSession session) {
        OrderForm orderForm = orderFormDao.findByID(orderFormID,student.getUniversityID());
        if(orderForm==null){
            return "查询错误";
        }
        orderForm.setStatus("已拒绝");
        int res = orderFormDao.updateStatus(orderForm);
        if(res!=1){
            return "error";
        }
        return "success";
    }

    @Override
    public String studentApprovalOrder(Student student, String orderFormID, HttpSession session) {
        OrderForm orderForm = orderFormDao.findByID(orderFormID,student.getUniversityID());
        if(orderForm==null){
            return "查询错误";
        }
        orderForm.setStatus("待付款");
        int res = orderFormDao.updateStatus(orderForm);
        if(res!=1){
            return "error";
        }
        return "success";
    }

    private OrderFormImpl orderFormToOrderFormImpl(OrderForm orderForm){
        System.out.println(orderForm);
        OrderFormImpl orderFormImpl = new OrderFormImpl();
        orderFormImpl.setOrderForm(orderForm);

        Student seller = studentDao.findByStudentID(orderForm.getSellerID(),orderForm.getUniversityID());
        System.out.println("seller"+seller);
        if(seller!=null){
            orderFormImpl.setSeller(seller);
        }
        Student buyer = studentDao.findByStudentID(orderForm.getBuyerID(),orderForm.getUniversityID());
        System.out.println("buyer"+buyer);
        if(buyer!=null){
            orderFormImpl.setBuyer(buyer);
        }
        Ebick ebick = ebickDao.findByID(orderForm.getEbickID(),orderForm.getUniversityID());
        System.out.println("ebick"+ebick);
        if(ebick!=null){
            orderFormImpl.setEbick(ebick);
        }

        EbickGoods ebickGoods = ebickGoodsDao.findByEbickID(orderForm.getEbickID(),orderForm.getUniversityID());
        System.out.println("ebickGoods"+ebickGoods);
        if(ebickGoods!=null){
            orderFormImpl.setEbickGoods(ebickGoods);
        }

        return orderFormImpl;
    }

    @Override
    public String adminGetAllOrders(Admin admin, HttpSession session) {

        List<OrderForm> orderFormList = orderFormDao.findByUniversityID(admin.getUniversityID());
        System.out.println(orderFormList);
        List<OrderFormImpl> orderFormImplList = new ArrayList<>();
        for(OrderForm orderForm : orderFormList){
            OrderFormImpl orderForm1 = orderFormToOrderFormImpl(orderForm);
            orderFormImplList.add(orderForm1);
        }
        System.out.println(orderFormImplList);
        session.setAttribute("orderFormImplList",orderFormImplList);
        return "success";
    }

    @Override
    public String adminGetFinishenOrdersNum(Admin admin, HttpSession session) {
        List<OrderForm> orderFormList = orderFormDao.findByStatus("已完成",admin.getUniversityID());
        int finishedOrderNum = orderFormList.size();
        session.setAttribute("finishedOrderNum",finishedOrderNum);
        return "success";
    }

    @Override
    public String schoolCounsellorGetAllOrders(SchoolCounsellor schoolCounsellor, HttpSession session) {

        List<OrderForm> orderFormList = orderFormDao.findByUniversityID(schoolCounsellor.getUniversityID());
        //System.out.println(orderFormList);
        List<OrderFormImpl> orderFormImplList = new ArrayList<>();
        for(OrderForm orderForm : orderFormList){
            Student student = studentDao.findByStudentID(orderForm.getSellerID(),schoolCounsellor.getUniversityID());
            if(student!=null && student.getMajorID().equals(schoolCounsellor.getMajorID())){
                OrderFormImpl orderForm1 = orderFormToOrderFormImpl(orderForm);
                orderFormImplList.add(orderForm1);
            }
            student = studentDao.findByStudentID(orderForm.getBuyerID(),schoolCounsellor.getUniversityID());
            if(student!=null && student.getMajorID().equals(schoolCounsellor.getMajorID())){
                OrderFormImpl orderForm1 = orderFormToOrderFormImpl(orderForm);
                orderFormImplList.add(orderForm1);
            }
        }
        //System.out.println(orderFormImplList);
        session.setAttribute("orderFormImplList",orderFormImplList);
        return "success";

    }

    @Override
    public String adminDeleteOneOrderForm(Admin admin, String orderFormID, HttpSession session) {
        int res = orderFormDao.delete(orderFormID,admin.getUniversityID());
        if(res!=1){
            return "删除失败";
        }
        return "success";
    }

    @Override
    public String schoolCounsellorGetFinishedOrderNum(SchoolCounsellor schoolCounsellor, HttpSession session) {
        List<OrderForm> orderFormList = orderFormDao.findByStatus("已完成",schoolCounsellor.getUniversityID());
        int finishedOrdersNum = 0;
        for(OrderForm orderForm : orderFormList){
            Student student = studentDao.findByStudentID(orderForm.getBuyerID(),schoolCounsellor.getUniversityID());
            if(student.getMajorID().equals(schoolCounsellor.getMajorID()))finishedOrdersNum++;
            student = studentDao.findByStudentID(orderForm.getSellerID(),schoolCounsellor.getUniversityID());
            if(student.getMajorID().equals(schoolCounsellor.getMajorID()))finishedOrdersNum++;
        }
        session.setAttribute("finishedOrdersNum",finishedOrdersNum);
        return "success";
    }
}
